package com.js.basicutils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * created by JS
 */
public class StringUtils {

	public StringUtils() {
	}

	public static boolean stringFilter(String regEx, String str) throws PatternSyntaxException {

		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		boolean flag = m.matches();
		return flag;
	}

	public static boolean isETValid(String str) {
		// 只允许汉字输入
		// String regEx = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]{1,15}+$";
		// 只允许字母、数字和汉字
		String regEx = "[^a-zA-Z0-9\u4E00-\u9FA5]";
		// String regEx = "^[a-zA-Z0-9\u4E00-\u9FA5]+$";
		return stringFilter(regEx, str);
	}

	/**
	 * @param regex
	 *            正则表达式字符串
	 * @param str
	 *            要匹配的字符串
	 * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
	 */
	private static boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	// "您输入的密码必须是字母和数字的组合"
	public static boolean isUserNameValid(String pwd) {
		// String reg = "[a-zA-Z0-9]{6,18}";
		String reg = "[a-zA-Z0-9]";
		return match(reg, pwd);
	}

	// 检查是否字符
	public static boolean isCharValid(String pwd) {
		// String reg = "[a-zA-Z0-9]{6,18}";
		String reg = "[a-zA-Z]";
		return match(reg, pwd);
	}

	// 检查是否数字
	public static boolean isNumberValid(String pwd) {
		String reg = "[0-9]";
		return match(reg, pwd);
	}

	/**
	 * 判断有没有中文
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNoChinese(String str) {

		for (int i = 0; i < str.length(); i++) {
			String tem = str.substring(i, i + 1);
			Pattern pattern = Pattern.compile("[0-9]*");
			Pattern pattern1 = Pattern.compile("[a-zA-Z]");
			Matcher isChar = pattern1.matcher(tem);
			Matcher isNum = pattern.matcher(tem);
			if (!isNum.matches()) {
				if (!isChar.matches()) {
					return false;
				}
			}
		}

		return true;
	}

	/**
	 * 判空，包括null
	 * @param value
	 * @return
	 */
	public static final boolean isNullOrEmpty(final String value) {
		if ("null".equalsIgnoreCase(value)) {
			return true;
		}
		return TextUtils.isEmpty(value);
	}

	/**
	 * 字符串转浮点型
	 * @param str
	 * @return
	 */
	public static float stringToFloat(String str) {
		if (!isNullOrEmpty(str)) {
			return Float.parseFloat(str);
		}
		return 0;
	}

	/**
	 * 判断是否是数字
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {

		Pattern pattern = Pattern.compile("[0-9]*");

		Matcher isNum = pattern.matcher(str);

		if (!isNum.matches()) {

			return false;

		}

		return true;
	}

			
	/**
	 * 证有1-3位小数的正实数
	 * @author： xudengqiao
	 * @date： 2018-3-16
	 * @param str
	 * @return
	 * @description：
	 */
	public static boolean isNumeric3(String str) {

		Pattern pattern = Pattern.compile("[0-9]+(.[0-9]{1,3})?");

		Matcher isNum = pattern.matcher(str);

		if (!isNum.matches()) {

			return false;

		}

		return true;
	}
	
	/**
	 * 判断年(1900-2099)
	 * @author： xudengqiao
	 * @date： 2018-3-16
	 * @param str
	 * @return
	 * @description：
	 */
	public static boolean isYear(String str) {
		
		Pattern pattern = Pattern.compile("1[9]\\d{2}|20[0-9][0-9]");
		Matcher isNum = pattern.matcher(str);

		if (!isNum.matches()) {

			return false;

		}

		return true;
	}
	
	/**
	 * 
	 * @author： 
	 * @date： 2018-1-9
	 * @param str
	 * @return
	 * @description：判断IP格式是否正确
	 */
	public static boolean isIp(String str) {
		String reg = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
				+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
		return match(reg, str);
	}

	/**
	 * 判断是否Double类型
	 * @param str
	 * @return
	 */
	public static boolean isDouble(String str) {
		try {
			Double.parseDouble(str);
		} catch (NumberFormatException ex) {
			return false;
		}
		return true;
	}

	/**
	 * 判断是否电话号码
	 * @param str
	 * @return
	 */
	public static boolean isEffectivePhone(String str) {

		Pattern pattern = Pattern.compile("^(((13[0-9])|(14[579])|(15([0-3]|[5-9]))|(16[6])|(17[0-9])|(18[0-9])|(19[89]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
		Pattern pattern2 = Pattern.compile("^0\\d{2,3}-?\\d{6,8}");

		Matcher isNum = pattern.matcher(str);
		Matcher isNum1 = pattern2.matcher(str);

		if (str.startsWith("1")) {
			if (!isNum.matches()) {

				return false;

			}
		} else if (str.startsWith("0")) {
			if (!isNum1.matches()) {

				return false;

			}
		} else {
			return false;
		}

		return true;
	}

	public static boolean isAlpha(String alpha) {
		String reg = "[0-9]{1,3}";
		return match(reg, alpha);
	}

	/**
	 * 
	 * @author： 
	 * @date： 2017-12-20
	 * @param context
	 * @return
	 * @description： 获取手机IMEI号,无IMEI就获取ID
	 */
	public static String getIMEI(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
		String imei = telephonyManager.getDeviceId();
		if (null == imei) {
			imei = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
		}
		return imei;
	}

	/**
	 * 
	 * @author： 
	 * @date： 2017-12-20
	 * @param context
	 * @param pkgname  应用包名，例如com.whty.wireless.sc
	 * @return
	 * @description：获取版本号
	 */
	public static int getVersionCode(Context context, String pkgname) {
		int versionCode = 0;
		try {
			versionCode = context.getPackageManager().getPackageInfo(pkgname, PackageManager.GET_ACTIVITIES).versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionCode;
	}

	/**
	 *
	 * @param context
	 * @param pkgname 应用包名，例如com.whty.wireless.sc
	 * @return
	 * @description：
	 */
	public static String getVersionName(Context context, String pkgname) {
		String versionName = "";
		try {
			versionName = context.getPackageManager().getPackageInfo(pkgname, PackageManager.GET_ACTIVITIES).versionName;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return versionName;
	}

	public static void toggleInputMethod(Context context) {
		InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 *
	 * @return
	 * @description：获得一个UUID 
	 */
	public static String getUUID() {
		String uuid = UUID.randomUUID().toString();
		// 去掉“-”符号
		return uuid.replaceAll("-", "");
	}

	/**
	 * 解析本地txt数据
	 * @param fileName
	 * @return
	 */
	public static String getFromLocal(String fileName) {
		try {
			StringBuffer sb = new StringBuffer();
			File file = new File(fileName);

			BufferedReader bufReader = new BufferedReader(new FileReader(file));
			String line = "";
			String Result = "";
			while ((line = bufReader.readLine()) != null)
				Result += line;
			return Result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public static String getCurrentDate() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = formatter.format(new Date());
		return time;
	}

	public static JSONObject stringToJsonObject(String json) {
		JSONObject jsonObject = null;
		if (!TextUtils.isEmpty(json)) {
			try {
				jsonObject = new JSONObject(json);
			} catch (JSONException e) {
				e.printStackTrace();
				return null;
			}
		}
		return jsonObject;
	}

	/**
	 * 当浮点型数据位数超过10位之后，数据变成科学计数法显示。用此方法可以使其正常显示。
	 * @param value
	 * @return Sting
	 */
	public static String formatFloatNumber(double value) {
		if(value != 0.00){
			java.text.DecimalFormat df = new java.text.DecimalFormat("########.00");
			return df.format(value);
		}else{
			return "0.00";
		}

	}
	public static String formatFloatNumber(Double value) {
		if(value != null){
			if(value.doubleValue() != 0.00){
				java.text.DecimalFormat df = new java.text.DecimalFormat("########.00");
				return df.format(value.doubleValue());
			}else{
				return "0.00";
			}
		}
		String a = "sfaf";
		return "";
	}

	/**
	 * 去除字符串开头的0
	 * @param s
	 * @return
	 */
	public static String removeStart0String(String s) {
		//方法1：String ——> Integer
		//String str = String.valueOf(Integer.parseInt(s));
		//方法2：正则
		//String str = s.replaceAll("^(0+)", "");
		//方法3：匹配开头
		if (s != null) return s.replaceFirst("0*", "");
		else return "";
	}

	/**
	 * 去除字符串末尾的0
	 * @param s
	 * @return
	 */
	public static String removeEnd0String(String s) {
		if (s != null) return s.replaceAll("(0)+$", "");
		else return "";
	}
}
